package dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import lgq.service.CourseManager;
import lgq.service.TeacherManager;
import lgq.service.TrainManager;
import model.Course;
import model.Teacher;
import model.Train;
import model.TrainCourse;
import util.DBUtil;
import util.Page;
import dao.TrainCourseDAO;

public class TrainCourseDAOImpl implements TrainCourseDAO {
	DBUtil dbu = new DBUtil();
	Connection conn = null;
	PreparedStatement pstat = null;
	ResultSet rs = null;

	public boolean addTrainCourse(TrainCourse trainCourse) {
		boolean flag = false;
		try {
			conn = dbu.getConnection();
			String sql = "insert into TRAINCOURSE(ID,TRAINID,COURSEID,PERCENT,TEACHERID) values(TRAINCOURSE_ID.nextval,?,?,?,?)";
			pstat = conn.prepareStatement(sql);
			pstat.setInt(1, trainCourse.getTrain().getId());
			pstat.setInt(2, trainCourse.getCourse().getId());
			pstat.setDouble(3, trainCourse.getPercent());
			pstat.setInt(4, trainCourse.getTeacher().getId());
			int i = pstat.executeUpdate();
			if (i > 0) {
				flag = true;
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			dbu.closeConnection(conn, pstat, rs);
		}
		return flag;
	}

	public boolean deleteByCourse(Course course) {
		// TODO Auto-generated method stub
		return false;
	}

	public boolean deleteById(int id) {
		// TODO Auto-generated method stub
		return false;
	}

	public boolean deleteByTeacher(Teacher teacher) {
		// TODO Auto-generated method stub
		return false;
	}

	public boolean deleteByTrain(Train id) {
		// TODO Auto-generated method stub
		return false;
	}

	public Page<TrainCourse> getAll(int pageNo, int pageSize) {
		// TODO Auto-generated method stub
		return null;
	}

	public List<TrainCourse> getByCourse(int pageNo, int pageSize, Course course) {
		// TODO Auto-generated method stub
		return null;
	}

	public TrainCourse getById(int id) {
		DBUtil dbu = new DBUtil();
		Connection conn = null;
		PreparedStatement pstat = null;
		ResultSet rs = null;
		TrainCourse tc=null;
		try {
			conn = dbu.getConnection();
			String sql = "select * from TRAINCOURSE where id=?";
			pstat = conn.prepareStatement(sql);
			pstat.setInt(1, id);
			rs=pstat.executeQuery();
			while(rs.next())
			{
				tc=new TrainCourse();
				tc.setId(rs.getInt("ID"));
				//通过train id获得train对象
				tc.setTrain(new TrainDAOImpl().getById(rs.getInt("TRAINID")));
				tc.setCourse(new CourseDAOImpl().getById(id));
				tc.setPercent(rs.getDouble("PERCENT"));
				tc.setTeacher(new TeacherDAOImpl().getById(rs.getInt("TEACHERID")));
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			dbu.closeConnection(conn, pstat, rs);
		}
		return tc;
	}

	public List<TrainCourse> getByTeacher(int pageNo, int pageSize,
			Teacher teacher) {
		// TODO Auto-generated method stub
		return null;
	}

	public List<TrainCourse> getByTrain(int pageNo, int pageSize, Train train) {
		// TODO Auto-generated method stub
		return null;
	}

	public boolean updateTrainCourse(TrainCourse trainCourse) {
		// TODO Auto-generated method stub
		return false;
	}

	public List<TrainCourse> getByTrainId(int id) {
		List<TrainCourse> list = new ArrayList<TrainCourse>();
		try {
			conn = dbu.getConnection();
			String sql = "select * from traincourse where trainid=?";
			pstat = conn.prepareStatement(sql);
			pstat.setInt(1, id);
			rs = pstat.executeQuery();
			while (rs.next()) {
				TrainCourse tc = new TrainCourse();
				tc.setId(rs.getInt("ID"));
				
				Train t = new Train();
				t.setId(id);
				tc.setTrain(t);
				tc.setCourse(new CourseManager().getById(rs.getInt("COURSEID")));
				tc.setPercent(rs.getDouble("PERCENT"));
				tc.setTeacher(new TeacherManager().getById(rs.getInt("TEACHERID")));
				list.add(tc);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			dbu.closeConnection(conn, pstat, rs);
		}
		return list;
	}

	public List<TrainCourse> getByTrainIdAndEmpid(int empid, int trainid) {
		List<TrainCourse> list = new ArrayList<TrainCourse>();
		try {
			conn = dbu.getConnection();
			String sql = "select * from traincourse where empid=? and trainid=?";
			pstat = conn.prepareStatement(sql);
			pstat.setInt(1, empid);
			pstat.setInt(2, trainid);
			rs = pstat.executeQuery();
			while (rs.next()) {
				TrainCourse tc = new TrainCourse();
				tc.setId(rs.getInt("ID"));				
				Train t = new Train();				
				t=new TrainDAOImpl().getById(trainid);
				tc.setTrain(t);
				tc.setCourse(new CourseManager().getById(rs.getInt("COURSEID")));
				tc.setPercent(rs.getDouble("PERCENT"));
				tc.setTeacher(new TeacherManager().getById(rs.getInt("TEACHERID")));
				list.add(tc);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			dbu.closeConnection(conn, pstat, rs);
		}
		return list;
	}
}
